package com.tq68.weixin.controller.commchat;

import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.tq68.weixin.common.AppConstantEnum;
import com.tq68.weixin.common.interf.IBaseController;
import com.tq68.weixin.model.Bbs;
import com.tq68.weixin.model.Comments;
import com.tq68.weixin.model.ForumPic;
import com.tq68.weixin.model.Sayyes;
import com.tq68.weixin.model.Seller;
import com.tq68.weixin.model.Tags;
import com.tq68.weixin.rules.ContributionValueRank;
import com.tq68.weixin.rules.GoldCoin;
import com.tq68.weixin.utils.Base64ForFile;
import com.tq68.weixin.utils.DateUtils;
import com.tq68.weixin.vo.UserVO;

public class CommunityChatController extends Controller implements IBaseController{

	public void index() {
		Integer category = getParaToInt("category",1);
		setAttr("category",category);
		setAttr("tags", Tags.dao.getTagsList(1));
		UserVO uservo = getSessionAttr("uservo");
		setAttr("convs",Bbs.dao.getConvList(uservo.getUserCommunityId()));
		render("index.html");
	}
	
	public void mainIndex() {
		render("mainIndex.html");
	}
	
	public void list(){
		UserVO uservo = getSessionAttr("uservo");
		Integer currentPage = getParaToInt("currentPage",1);
		String path = getAttrForStr("imgPath");
		Integer category = getParaToInt("category");
		Integer commuid = uservo.getUserCommunityId();
		String homeTown2 = uservo.getProvince()+" "+uservo.getCity();
		setAttr("datalist", Bbs.dao.getBbsList(currentPage, AppConstantEnum.PAGE_SIZE,uservo.getUserid(),path,category,commuid,homeTown2));
		render("_list.html");
	}

	public void add() {
		List<Tags> tags = Tags.dao.getTagsList(1);
		setAttr("tags", tags);
		keepPara("category");
		render("add.html");
	}
	@Before(Tx.class)
	public void addEntity() {
		String[] base64Imgs = getParaValues("base64Img");
		String comments = getPara("comments");
		Integer tag = getParaToInt("tag");//第一层tag
		String tagname = getPara("tagname");
		UserVO uservo = getSessionAttr("uservo");
		Integer bbsid = 0;
		Integer forumSubject = Integer.parseInt(AppConstantEnum.SubjectType.Others.getCode()); //其他类主题
		Integer userid = uservo.getUserid();
		String nickname = uservo.getNickname();
		Integer userlevel = uservo.getUserlevel();
		Bbs bbs = new Bbs();
		if(base64Imgs==null){//只发文字
			bbs.setForumSubject(forumSubject);
			bbs.setContent(comments);
			bbs.setAddtime(DateUtils.toTimeStr(new Date()));
			bbs.setMainpicPath("");
			bbs.setUserid(userid);
			bbs.setNickname(nickname);
			bbs.setUserlevel(userlevel);
			bbs.setTagsname(tagname);
			bbs.setCategory(tag);
			bbs.setCommuid(uservo.getUserCommunityId());
			bbs.setCommuName(uservo.getUserCommunityName());
			bbs.setStatus(Integer.parseInt(AppConstantEnum.BbsStatus.Active.getCode()));
			bbs.save();
			//保存标签
//			String [] tagsArray = tags.split(", ");
//			for(String id:tagsArray){
//				TagBbs tag = new TagBbs();
//				tag.setBbsid(bbs.getId());
//				tag.setTagid(Integer.parseInt(id));
//				tag.save();
//			}
		}else{//文字加图片
			for(int i=0;i<base64Imgs.length;i++){
				ForumPic pic = new ForumPic();
				//对字节数组字符串进行Base64解码并生成图片
				String fileName = Base64ForFile.GenerateImage(base64Imgs[i]);
				if(i==0){
					bbs.setForumSubject(forumSubject);
					bbs.setContent(comments);
					bbs.setAddtime(DateUtils.toTimeStr(new Date()));
					bbs.setMainpicPath(fileName);
					bbs.setUserid(userid);
					bbs.setNickname(nickname);
					bbs.setUserlevel(userlevel);
					bbs.setTagsname(tagname);
					bbs.setCategory(tag);
					bbs.setCommuid(uservo.getUserCommunityId());
					bbs.setCommuName(uservo.getUserCommunityName());
					bbs.setStatus(Integer.parseInt(AppConstantEnum.BbsStatus.Active.getCode()));
					bbs.save();
					bbsid = bbs.getId();
					//保存标签
//					String [] tagsArray = tags.split(", ");
//					for(String id:tagsArray){
//						TagBbs tag = new TagBbs();
//						tag.setBbsid(bbs.getId());
//						tag.setTagid(Integer.parseInt(id));
//						tag.save();
//					}
					pic.setType(Integer.parseInt(AppConstantEnum.BbsPicType.Main.getCode()));
				}else{
					pic.setType(Integer.parseInt(AppConstantEnum.BbsPicType.Detail.getCode()));
				}
				pic.setBbsid(bbsid);
				pic.setFilename(fileName);
				pic.save();
			}
		}
		//业主发帖计算经验值
		ContributionValueRank cv = new ContributionValueRank();
		cv.newPost(userid);
		//业主发帖计算金币奖励
		GoldCoin gc = new GoldCoin();
		gc.addForNewPost(userid);
		redirect("/communityChat?category="+tag);
	}

	public void edit() {
		
	}

	public void editEntity() {
		
	}

	public void deleteEntity() {
		Bbs bbs = Bbs.dao.findById(getParaToInt("bbsid",0));
		if(bbs != null){
			bbs.delete();
			//删除图片
			//删除评论
			//删除点赞数
		}
		redirect("/communityChat");
	}
	@Before(Tx.class)
	public void addComment(){
		UserVO uservo = getSessionAttr("uservo");
		Comments comments = getModel(Comments.class);
		comments.setAddtime(DateUtils.toTimeStr(new Date()));
		comments.setUserid(uservo.getUserid());
		comments.setNickname(uservo.getNickname());
		comments.setUserlevel(uservo.getUserlevel());
		comments.save();
		//update comments count
		Bbs bbs = Bbs.dao.findById(comments.getBbsid());
		bbs.setCommentCount(bbs.getCommentCount()+1);//评论统计
		bbs.setActiveCount(bbs.getActiveCount()+1);//活跃量统计
		bbs.update();
		//评论成功，对业主增加贡献值
		ContributionValueRank cv = new ContributionValueRank();
		cv.giveComment(uservo.getUserid());
		renderText("OK");
	}
	
	@Before(Tx.class)
	public void addYes(){
		UserVO uservo = getSessionAttr("uservo");
		Sayyes yes = getModel(Sayyes.class);
		yes.setUserid(uservo.getUserid());
		yes.setNickname(uservo.getNickname());
		Sayyes sayyes = Sayyes.dao.findByAttr(yes);
		if(sayyes != null){
			//已点过赞
			sayyes.setSayyes(yes.getSayyes());;
			sayyes.setLastEditTime(DateUtils.toTimeStr(new Date()));
			sayyes.update();
			yes = sayyes;
		}else{
			yes.setLastEditTime(DateUtils.toTimeStr(new Date()));
			yes.save();
			//点赞成功，对业主增加贡献值
			ContributionValueRank cv = new ContributionValueRank();
			cv.clickLike(uservo.getUserid());
			//帖子被点赞之后，调用此方法，判断是否需要对发帖业主或商家奖励金币数量，并更新
			GoldCoin gc = new GoldCoin();
			gc.addforLikeSummary(yes.getYesTargetid());
			//优惠信息被点赞时，增加统计数据到t_seller表
			Bbs bbs = Bbs.dao.findById(yes.getYesTargetid());
			if(Integer.parseInt(AppConstantEnum.SubjectType.Discounts.getCode())==bbs.getForumSubject()){
				Seller seller = Seller.dao.findById(bbs.getUserid());
				seller.setYesCount(seller.getYesCount()+1);
				seller.update();
			}
		}
		if(yes.getYesTargetType() == Integer.parseInt(AppConstantEnum.SayYesTargetType.Bbs.getCode())){
			//update bbs yes count
			Bbs bbs = Bbs.dao.findById(yes.getYesTargetid());
			if(yes.getSayyes()==1){//由取消赞-->点赞
				bbs.setYescount(bbs.getYescount()+1);
				bbs.setActiveCount(bbs.getActiveCount()+1);//活跃量统计
			}else{//由点赞-->取消赞
				//bbs.setYescount(bbs.getYescount()-1);
			}
			bbs.update();
		}else if(yes.getYesTargetType() == Integer.parseInt(AppConstantEnum.SayYesTargetType.BbsComment.getCode())){
			//update comment yes count
			
		}
		renderText("OK");
	}

	public void clickPicture(){
		Integer bbsid = getParaToInt("bbsid",0);
		Bbs bbs = Bbs.dao.findById(bbsid);
		if(bbs!=null){
			bbs.setActiveCount(bbs.getActiveCount()+1);//活跃量统计
			bbs.update();
			renderText("OK");
		}else{
			renderText("ERROR");
		}
	}
}
